// pages/transferDetail/transferDetail.js
var util = require('../../utils/util.js');
import tips from '../../lib/tips';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    transfer: null,
    tipsData: {
      title: '',
      isHidden: true
    },
    baseDomain: app.globalData.baseDomain,
    apiDomain: app.globalData.apiDomain,
    imageList:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var transferId = options.transferId;
    wx.request({
      url: app.globalData.apiDomain + '/api/transfer/getTransferDetail/' + transferId,
      success(res) {
        var obj = JSON.parse(res.data.data.contentImg);
        that.setData({
          transfer: res.data.data,
          imageList: obj
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  showToast(title, duartion) {
    const that = this;
    const tipsData = {
      title: title || '',
      duartion: duartion || 2000,
      isHidden: false
    };
    tips.toast(that.data.tipsData);
    that.setData({
      tipsData
    });
    setTimeout(() => {
      tipsData.isHidden = true;
      that.setData({
        tipsData
      });
    }, tipsData.duartion);
  },
  buyNow: util.throttle(function(e) {
    const that = this;
    var openId = e.currentTarget.dataset.openId;
    if (app.globalData.openId == openId){
      that.showToast("您不能入手自己发布的宝贝！", 2000);
      return;
    }
    wx.request({
      url: app.globalData.apiDomain + '/api/address/find',
      method: "POST",
      data: {
        openId: app.globalData.openId
      },
      success: function(res) {
        if (!res.data.data || res.data.data.length == 0) {
          that.showToast("您还没有设置收货地址", 2000);
          wx.navigateTo({
            url: '../address-edit/address-edit?resource=2&transferId=' + that.data.transfer.id,
          })
        } else {
          wx.navigateTo({
            url: '../confirmOrder/confirmOrder?transferId=' + that.data.transfer.id,
          })
        }
      }
    })
  })

})